package com.hlk.article.utils;

import com.hlk.enums.ResponseStatusEnum;
import com.hlk.exception.HuaException;
import com.hlk.feign.UserFeignClient;
import com.hlk.pojo.vo.AppUserVO;
import com.hlk.pojo.vo.ArticleDetailVO;
import com.hlk.result.Result;
import com.hlk.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Set;

/**
 * @author hlk
 * @version 1.0
 * @date 2021-01-08 15:09
 */
@Component
public class RestTemplateUtils {
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 发起远程调用，获得用户的基本信息
     *
     * @param idSet
     * @return
     */
    public List<AppUserVO> getPublisherList(Set idSet) {
        String userServerUrlExecute
                = "http://user.hlknews.com:8003/user/queryByIds?userIds=" + JsonUtils.objectToJson(idSet);
        ResponseEntity<Result> responseEntity
                = restTemplate.getForEntity(userServerUrlExecute, Result.class);
        Result bodyResult = responseEntity.getBody();
        List<AppUserVO> publisherList = null;
        if (bodyResult.getStatus() == 200) {
            String userJson = JsonUtils.objectToJson(bodyResult.getData());
            publisherList = JsonUtils.jsonToList(userJson, AppUserVO.class);
        }
        return publisherList;
    }

    /**
     * 发起远程调用rest，获得文章详情数据
     */
    public ArticleDetailVO getArticleDetail(String articleId) {
        String url
                = "http://www.hlknews.com:8001/portal/article/detail?articleId=" + articleId;
        ResponseEntity<Result> responseEntity
                = restTemplate.getForEntity(url, Result.class);
        Result bodyResult = responseEntity.getBody();
        ArticleDetailVO detailVO = null;
        if (bodyResult.getStatus() == 200) {
            String detailJson = JsonUtils.objectToJson(bodyResult.getData());
            detailVO = JsonUtils.jsonToPojo(detailJson, ArticleDetailVO.class);
        }
        return detailVO;
    }

    /**
     * 下载文章静态页面
     *
     * @param articleId
     * @param articleMongoId
     */
    public void downloadArticleHtml(String articleId, String articleMongoId) {
        String url = "http://html.hlknews.com:8002/article/html/download?articleId=" + articleId + "&articleMongoId=" + articleMongoId;
        ResponseEntity<Integer> responseEntity = restTemplate.getForEntity(url, Integer.class);
        int status = responseEntity.getBody();
        if (status != HttpStatus.OK.value()) {
            throw new HuaException(ResponseStatusEnum.ARTICLE_REVIEW_ERROR);
        }
    }

    /**
     * 下载文章静态页面
     *
     * @param articleId
     */
    public void deleteArticleHtml(String articleId) {
        String url = "http://html.hlknews.com:8002/article/html/delete?articleId=" + articleId;
        ResponseEntity<Integer> responseEntity = restTemplate.getForEntity(url, Integer.class);
        int status = responseEntity.getBody();
        if (status != HttpStatus.OK.value()) {
            throw new HuaException(ResponseStatusEnum.ARTICLE_DELETE_ERROR);
        }
    }
}
